<template>
  <el-dialog
    :model-value="modelValue"
    @update:model-value="(value) => emit('update:modelValue', value)"
    :title="isEdit ? '编辑巡视管理' : '新增巡视管理'"
    width="800px"
    :close-on-click-modal="false"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="120px"
      size="default"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="巡视编号" prop="inspectionNo">
            <el-input v-model="form.inspectionNo" placeholder="请输入巡视编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="巡视日期" prop="inspectionDate">
            <el-date-picker
              v-model="form.inspectionDate"
              type="date"
              placeholder="选择巡视日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="巡视类型" prop="inspectionType">
            <el-select v-model="form.inspectionType" placeholder="选择巡视类型" style="width: 100%">
              <el-option label="日常巡视" value="daily" />
              <el-option label="专项巡视" value="special" />
              <el-option label="应急巡视" value="emergency" />
              <el-option label="定期巡视" value="regular" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="巡视区域" prop="inspectionArea">
            <el-input v-model="form.inspectionArea" placeholder="请输入巡视区域" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="巡视人员" prop="inspector">
            <el-input v-model="form.inspector" placeholder="请输入巡视人员" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="开始时间" prop="startTime">
            <el-time-picker
              v-model="form.startTime"
              placeholder="选择开始时间"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="endTime">
            <el-time-picker
              v-model="form.endTime"
              placeholder="选择结束时间"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="天气情况" prop="weather">
            <el-select v-model="form.weather" placeholder="选择天气情况" style="width: 100%">
              <el-option label="晴" value="sunny" />
              <el-option label="多云" value="cloudy" />
              <el-option label="阴" value="overcast" />
              <el-option label="小雨" value="light_rain" />
              <el-option label="中雨" value="moderate_rain" />
              <el-option label="大雨" value="heavy_rain" />
              <el-option label="雪" value="snow" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="巡视状态" prop="inspectionStatus">
            <el-select v-model="form.inspectionStatus" placeholder="选择巡视状态" style="width: 100%">
              <el-option label="已完成" value="completed" />
              <el-option label="进行中" value="in_progress" />
              <el-option label="待开始" value="pending" />
              <el-option label="已暂停" value="suspended" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="巡视路线" prop="inspectionRoute">
            <el-input
              v-model="form.inspectionRoute"
              type="textarea"
              :rows="3"
              placeholder="请输入巡视路线"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="巡视内容" prop="inspectionContent">
            <el-input
              v-model="form.inspectionContent"
              type="textarea"
              :rows="4"
              placeholder="请输入巡视内容"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="发现问题" prop="foundProblems">
            <el-input
              v-model="form.foundProblems"
              type="textarea"
              :rows="4"
              placeholder="请输入发现的问题"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="处理措施" prop="handlingMeasures">
            <el-input
              v-model="form.handlingMeasures"
              type="textarea"
              :rows="4"
              placeholder="请输入处理措施"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :rows="3"
              placeholder="请输入备注信息"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="emit('update:modelValue', false)">取消</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'

interface Props {
  modelValue: boolean
  inspectionData?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  inspectionData: null
})

const emit = defineEmits<Emits>()

const formRef = ref<FormInstance>()
const isEdit = ref(false)

const form = reactive({
  inspectionNo: '',
  inspectionDate: '',
  inspectionType: '',
  inspectionArea: '',
  inspector: '',
  contactPhone: '',
  startTime: '',
  endTime: '',
  weather: '',
  inspectionStatus: '',
  inspectionRoute: '',
  inspectionContent: '',
  foundProblems: '',
  handlingMeasures: '',
  remark: ''
})

const rules: FormRules = {
  inspectionNo: [
    { required: true, message: '请输入巡视编号', trigger: 'blur' }
  ],
  inspectionDate: [
    { required: true, message: '请选择巡视日期', trigger: 'change' }
  ],
  inspectionType: [
    { required: true, message: '请选择巡视类型', trigger: 'change' }
  ],
  inspectionArea: [
    { required: true, message: '请输入巡视区域', trigger: 'blur' }
  ],
  inspector: [
    { required: true, message: '请输入巡视人员', trigger: 'blur' }
  ],
  contactPhone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  inspectionStatus: [
    { required: true, message: '请选择巡视状态', trigger: 'change' }
  ]
}

// 重置表单
const resetForm = () => {
  Object.assign(form, {
    inspectionNo: '',
    inspectionDate: '',
    inspectionType: '',
    inspectionArea: '',
    inspector: '',
    contactPhone: '',
    startTime: '',
    endTime: '',
    weather: '',
    inspectionStatus: '',
    inspectionRoute: '',
    inspectionContent: '',
    foundProblems: '',
    handlingMeasures: '',
    remark: ''
  })
  formRef.value?.clearValidate()
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    
    // 这里应该调用API保存数据
    console.log('提交表单数据:', form)
    
    ElMessage.success(isEdit.value ? '修改成功' : '添加成功')
    emit('success')
    emit('update:modelValue', false)
  } catch (error) {
    ElMessage.error('请检查表单填写是否正确')
  }
}

// 监听弹窗打开，初始化表单数据
watch(() => props.modelValue, (newVal) => {
  if (newVal) {
    if (props.inspectionData) {
      isEdit.value = true
      Object.assign(form, props.inspectionData)
    } else {
      isEdit.value = false
      resetForm()
    }
  }
}, { immediate: true })
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>